<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'urutan', type:'numeric', precision:0 },
			{ name: 'kd_barang', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_barang').setval(''); 
					$('#<?php echo $ls_gridname; ?>_nama_barang').setval('');
				},
				onActionButton:function(){ 
					LOVBarang({ onSelect:function(row){ 
						$('#<?php echo $ls_gridname; ?>_kd_barang').setval(row.kd_barang); 
						$('#<?php echo $ls_gridname; ?>_nama_barang').setval(row.nama_barang); 
					} }); 
				}
			},
			{ name: 'nama_barang', type: 'text', size: '24', readonly:true },
			{ name: 'qty_order', type: 'numeric', precision:2 },
			{ name: 'qty_terima', type: 'numeric', precision:2 },
			{ name: 'harga', type: 'numeric', precision:2 }
		], {}, { prefix:'<?php echo $ls_gridname; ?>_' });
		
		// Set Hidden Object
		$('#<?php echo $ls_gridname; ?>_urutan').hide();
		
		// Create Dialog
		$('#form_<?php echo $ls_gridname; ?>_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:800,
			height: 500,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					var ls_mode			= $('#form_<?php echo $ls_gridname; ?>').data('mode');
					var row				= formToJSON('form_<?php echo $ls_gridname; ?>');
					var ls_error		= '';					
					// Validasi
					
					if(row.kd_barang == ''){
						ls_error	+= 'Isi Barang\n';
					}
					if(row.qty_order == 0){
						ls_error	+= 'Isi Jumlah Barang\n';
					}
					
					// Eksekusi
					if(ls_error!=''){
						$.messager.alert('Error', nl2br(ls_error), 'error');
					}else{
						if(ls_mode=='create'){
							$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
						} else {
							var selectedIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
							
							updateRowFlag('<?php echo $ls_gridname; ?>', row); // Patching Flag Update
							$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:selectedIndex,row:row});
							
							$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
							jsonToForm(oSel, {prefix:'<?php echo $ls_gridname; ?>_'});
							$('#<?php echo $ls_gridname; ?>_kd_barang').val( $('#<?php echo $ls_gridname; ?>_kd_barang').val() );
						}
					}
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#form_<?php echo $ls_gridname; ?>_container').dialog('close');
				}
			}]
		});
	});
</script>
<div id="form_<?php echo $ls_gridname; ?>_container" style="padding:5px;width:600px;height:400px;">
	<div id="form_<?php echo $ls_gridname; ?>">
    <input id="urutan" />
    
    <fieldset class="inline-block">
	<legend>Pemesanan</legend>
	<table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">  
		<tr>
			<td>Kemasan:<br /><input id="kd_barang" /></td>
			<td>Satuan:<br /><input id="kd_barang" /></td>
			<td>Qty Pesanan:<br /><input id="kd_barang" /></td>
			<td>Harga Beli:<br /><input id="kd_barang" /></td>
		</tr>
		<tr>
			<td>Kemasan:<br /><input id="kd_barang" /></td>
			<td>Satuan:<br /><input id="kd_barang" /></td>
			<td>Qty Pesanan Pcs:<br /><input id="kd_barang" /></td>
			<td>Harga Beli Pcs:<br /><input id="kd_barang" /></td>
		</tr>
	</table>
    </fieldset>
  
    <fieldset class="inline-block">
    	<legend>Diskon%</legend>
    	<table>
    		<tr>
    			<td>Diskon 1:<br /><input id="qty_terima" /></td>
    			<td>Diskon 2:<br /><input id="qty_terima" /></td>
    			<td>Diskon 3:<br /><input id="qty_terima" /></td>
    			<td>Diskon 4:<br /><input id="qty_terima" /></td>
    			<td>Diskon 5:<br /><input id="qty_terima" /></td>
    		</tr>
    	</table>
    </fieldset>

    <fieldset class="inline-block">
    	<legend>Penerimaan</legend>
    	<table>
    		<tr>
    			<td class="label">Qty Terima:<br/><input id="qty_terima" /></td>
    			<td class="label">Qty Pcs Terima:<br/><input id="qty_terima" /></td>
    			<td class="label">Total Qty:<br/><input id="qty_terima" /></td>
    			<td class="label">Harga Beli Satuan:<br/><input id="qty_terima" /></td>
    			<td class="label">Total:<br/><input id="qty_terima" /></td>
    		</tr>
    	</table>
    </fieldset> 

	<fieldset class="inline-block">
		<legend>Harga Barang</legend>
		<table>
			<tr>
				<td class="label">Total Setelah Diskon:<br /><input id="harga" /></td>
				<td class="label">PPN(10%):<br /><input type="checkbox" id="bkp" name="bkp" /></td>
				<td class="label">Harga Satuan:<br /><input id="harga" /></td>
			</tr>
		</table>
	</fieldset> 


	</div>
</div>
<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ no_po:$('#no_po').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Ubah',
				iconCls:'icon-edit',
				handler:function(){
					var oSel=$('#<?php echo $ls_gridname; ?>').datagrid('getSelected'); 
					if(oSel==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else{ 
						resetError();
						
						$('#form_<?php echo $ls_gridname; ?>_container').dialog('open');
						$('#form_<?php echo $ls_gridname; ?>').data('mode', 'edit');
						$('#form_<?php echo $ls_gridname; ?>_container').prev().find('.panel-title').html('Ubah Detail Barang');
						
						jsonToForm(oSel, {prefix:'<?php echo $ls_gridname; ?>_'});
						
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
	});
	
</script>
<table id="<?php echo $ls_gridname; ?>" style="width:600;height:auto" singleSelect="true" idField="urutan" showFooter="true">
<thead>
    <tr>
    	<th field="kd_barang" width="200">Kode Barang</th>
        <th field="nama_barang" width="200">Nama Barang</th>
        <th field="qty_order" width="120" align="right" formatter="formatNumericPoint2">Jumlah Order</th>
        <th field="qty_terima" width="120" align="right" formatter="formatNumericPoint2">Jumlah Terima</th>
        <th field="harga" width="120" align="right" formatter="formatNumericPoint2">Harga</th>
    </tr>
</thead>
</table>
